<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="/blog/lib/pace/pace-theme-minimal.min.css">
  <script src="/blog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/blog/","scheme":"Gemini","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="变量，关键字，数据类型，运算符">
<meta property="og:type" content="article">
<meta property="og:title" content="变量和类型">
<meta property="og:url" content="https://gitee.com/lzx411559669/2020/03/07/%E5%8F%98%E9%87%8F%E5%92%8C%E7%B1%BB%E5%9E%8B/index.html">
<meta property="og:site_name" content="刘正星的博客">
<meta property="og:description" content="变量，关键字，数据类型，运算符">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-03-07T14:10:17.000Z">
<meta property="article:modified_time" content="2020-03-07T14:28:08.701Z">
<meta property="article:author" content="刘正星">
<meta property="article:tag" content="-变量 -类型">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://gitee.com/lzx411559669/2020/03/07/%E5%8F%98%E9%87%8F%E5%92%8C%E7%B1%BB%E5%9E%8B/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true
  };
</script>

  <title>变量和类型 | 刘正星的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/blog/atom.xml" title="刘正星的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
<a href="https://github.com/lzx411559669" target="_blank" rel="noopener" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#64CEAA; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">刘正星的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">欢迎！</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/lzx411559669/2020/03/07/%E5%8F%98%E9%87%8F%E5%92%8C%E7%B1%BB%E5%9E%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/uploads/me.jpg">
      <meta itemprop="name" content="刘正星">
      <meta itemprop="description" content="一名不知名的小菜鸡">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="刘正星的博客">
    </span>
      <header class="post-header">
         
        <h1 class="post-title" itemprop="name headline">
          变量和类型
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-03-07 22:10:17 / 修改时间：22:28:08" itemprop="dateCreated datePublished" datetime="2020-03-07T22:10:17+08:00">2020-03-07</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/Java%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">Java基础</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7.9k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>7 分钟</span>
            </span>
            <div class="post-description">变量，关键字，数据类型，运算符</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <meta name="referrer" content="no-referrer"/>
# 变量和类型

<p><a name="8AwSV"></a></p>
<h1 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h1><p>变量：等同于一个有命名的存储地址</p>
<ul>
<li>变量是解决内容复用的</li>
<li>变量是有名称的</li>
</ul>
<p>java是面向对象的语言，把一切内容（字符串，数字，布尔，日期，时间等）都用对象表示，所以java世界中变量内容就是对象。<br><a name="2kdGa"></a></p>
<h2 id="Java语言支持的变量类型有："><a href="#Java语言支持的变量类型有：" class="headerlink" title="Java语言支持的变量类型有："></a>Java语言支持的变量类型有：</h2><ul>
<li>类变量：独立于方法之外的变量，用 static 修饰。</li>
<li>实例变量：独立于方法之外的变量，不过没有 static 修饰。</li>
<li>局部变量：类的方法中的变量。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Variable</span></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> allClicks=<span class="number">0</span>;    <span class="comment">// 类变量</span></span><br><span class="line"> </span><br><span class="line">    String str=<span class="string">"hello world"</span>;  <span class="comment">// 实例变量</span></span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">()</span></span>&#123;</span><br><span class="line"> </span><br><span class="line">        <span class="keyword">int</span> i =<span class="number">0</span>;  <span class="comment">// 局部变量</span></span><br><span class="line"> </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a name="gOQp4"></a></p>
<h2 id="类变量（静态变量）"><a href="#类变量（静态变量）" class="headerlink" title="类变量（静态变量）"></a>类变量（静态变量）</h2><ul>
<li><p>类变量也称为静态变量，在类中以 static 关键字声明，但必须在方法之外。</p>
</li>
<li><p>无论一个类创建了多少个对象，类只拥有类变量的一份拷贝。</p>
</li>
<li><p>静态变量除了被声明为常量外很少使用。常量是指声明为public/private，final和static类型的变量。常量初始化后不可改变。</p>
</li>
<li><p>静态变量储存在静态存储区。经常被声明为常量，很少单独使用static声明变量。</p>
</li>
<li><p>静态变量在第一次被访问时创建，在程序结束时销毁。</p>
</li>
<li><p>与实例变量具有相似的可见性。但为了对类的使用者可见，大多数静态变量声明为public类型。</p>
</li>
<li><p>默认值和实例变量相似。数值型变量默认值是0，布尔型默认值是false，引用类型默认值是null。变量的值可以在声明的时候指定，也可以在构造方法中指定。此外，静态变量还可以在静态语句块中初始化。</p>
</li>
<li><p>静态变量可以通过：_ClassName.VariableName_的方式访问。</p>
</li>
<li><p>类变量被声明为public static final类型时，类变量名称一般建议使用大写字母。如果静态变量不是public和final类型，其命名方式与实例变量以及局部变量的命名方式一致。<br><a name="yTQF1"></a></p>
<h2 id="Java-局部变量"><a href="#Java-局部变量" class="headerlink" title="Java 局部变量"></a>Java 局部变量</h2></li>
<li><p>局部变量声明在方法、构造方法或者语句块中；</p>
</li>
<li><p>局部变量在方法、构造方法、或者语句块被执行的时候创建，当它们执行完成后，变量将会被销毁；</p>
</li>
<li><p>访问修饰符不能用于局部变量；</p>
</li>
<li><p>局部变量只在声明它的方法、构造方法或者语句块中可见；</p>
</li>
<li><p>局部变量是在栈上分配的。</p>
</li>
<li><p>局部变量没有默认值，所以局部变量被声明后，必须经过初始化，才可以使用<br><a name="lUR4D"></a></p>
<h2 id="实例变量"><a href="#实例变量" class="headerlink" title="实例变量"></a>实例变量</h2></li>
<li><p>实例变量声明在一个类中，但在方法、构造方法和语句块之外；</p>
</li>
<li><p>当一个对象被实例化之后，每个实例变量的值就跟着确定；</p>
</li>
<li><p>实例变量在对象创建的时候创建，在对象被销毁的时候销毁；</p>
</li>
<li><p>实例变量的值应该至少被一个方法、构造方法或者语句块引用，使得外部能够通过这些方式获取实例变量信息；</p>
</li>
<li><p>实例变量可以声明在使用前或者使用后；</p>
</li>
<li><p>访问修饰符可以修饰实例变量；</p>
</li>
<li><p>实例变量对于类中的方法、构造方法或者语句块是可见的。一般情况下应该把实例变量设为私有。通过使用访问修饰符可以使实例变量对子类可见；</p>
</li>
<li><p>实例变量具有默认值。数值型变量的默认值是0，布尔型变量的默认值是false，引用类型变量的默认值是null。变量的值可以在声明时指定，也可以在构造方法中指定；</p>
</li>
<li><p>实例变量可以直接通过变量名访问。但在静态方法以及其他类中，就应该使用完全限定名：ObejectReference.VariableName。<br><a name="R8FG6"></a></p>
<h2 id="Java-中静态变量和实例变量区别"><a href="#Java-中静态变量和实例变量区别" class="headerlink" title="Java 中静态变量和实例变量区别"></a>Java 中静态变量和实例变量区别</h2></li>
<li><p>静态变量属于类，该类不生产对象，通过类名就可以调用静态变量。</p>
</li>
<li><p>实例变量属于该类的对象，必须产生该类对象，才能调用实例变量。</p>
</li>
</ul>
<p><strong>在程序运行时的区别：</strong></p>
<ul>
<li>实例变量属于某个对象的属性，必须创建了实例对象，其中的实例变量才会被分配空间，才能使用这个实例变量。</li>
<li>静态变量不属于某个实例对象，而是属于类，所以也称为类变量，只要程序加载了类的字节码，不用创建任何实例对象，静态变量就会被分配空间，静态变量就可以被使用了。</li>
</ul>
<p>总之，实例变量必须创建对象后才可以通过这个对象来使用，静态变量则可以直接使用类名来引用。<br />例如，对于下面的程序，无论创建多少个实例对象， 永远都只分配了一个 staticInt 变量，并且每创建一个实例对象，这个 staticInt 就会加 1；但是，每创建一个实例对象，就会分配一个 random， 即可能分配多个 random ，并且每个 random 的值都只自加了1次。<br><a name="av4vZ"></a></p>
<h2 id="成员变量、局部变量、静态变量的区别"><a href="#成员变量、局部变量、静态变量的区别" class="headerlink" title="成员变量、局部变量、静态变量的区别"></a>成员变量、局部变量、静态变量的区别</h2><table>
<thead>
<tr>
<th></th>
<th><strong>成员变量</strong></th>
<th><strong>局部变量</strong></th>
<th><strong>静态变量</strong></th>
</tr>
</thead>
<tbody><tr>
<td>定义位置</td>
<td>在类中,方法外</td>
<td>方法中,或者方法的形式参数</td>
<td>在类中,方法外</td>
</tr>
<tr>
<td>初始化值</td>
<td>有默认初始化值</td>
<td>无,先定义,赋值后才能使用</td>
<td>有默认初始化值</td>
</tr>
<tr>
<td>调用方式</td>
<td>对象调用</td>
<td>—</td>
<td>对象调用，类名调用</td>
</tr>
<tr>
<td>存储位置</td>
<td>堆中</td>
<td>栈中</td>
<td>方法区</td>
</tr>
<tr>
<td>生命周期</td>
<td>与对象共存亡</td>
<td>与方法共存亡</td>
<td>与类共存亡</td>
</tr>
<tr>
<td>别名</td>
<td>实例变量</td>
<td>—</td>
<td>类变量</td>
</tr>
</tbody></table>
<p><a name="oeJa0"></a></p>
<h1 id="关键字"><a href="#关键字" class="headerlink" title="关键字"></a>关键字</h1><p>常见关键字：</p>
<table>
<thead>
<tr>
<th align="left">abstract</th>
<th align="left">assert</th>
<th align="left">boolean</th>
<th align="left">break</th>
<th align="left">byte</th>
</tr>
</thead>
<tbody><tr>
<td align="left">case</td>
<td align="left">catch</td>
<td align="left">char</td>
<td align="left">class</td>
<td align="left">const</td>
</tr>
<tr>
<td align="left">continue</td>
<td align="left">default</td>
<td align="left">do</td>
<td align="left">double</td>
<td align="left">else</td>
</tr>
<tr>
<td align="left">enum</td>
<td align="left">extends</td>
<td align="left">final</td>
<td align="left">finally</td>
<td align="left">float</td>
</tr>
<tr>
<td align="left">for</td>
<td align="left">goto</td>
<td align="left">if</td>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=7686653&ss_c=ssc.citiao.link" target="_blank" rel="noopener">implements</a></td>
<td align="left">import</td>
</tr>
<tr>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=7762065&ss_c=ssc.citiao.link" target="_blank" rel="noopener">instanceof</a></td>
<td align="left">int</td>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=3389638&ss_c=ssc.citiao.link" target="_blank" rel="noopener">interface</a></td>
<td align="left">long</td>
<td align="left">native</td>
</tr>
<tr>
<td align="left">new</td>
<td align="left">package</td>
<td align="left">private</td>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=7667016&ss_c=ssc.citiao.link" target="_blank" rel="noopener">protected</a></td>
<td align="left">public</td>
</tr>
<tr>
<td align="left">return</td>
<td align="left">strictfp</td>
<td align="left">short</td>
<td align="left">static</td>
<td align="left">super</td>
</tr>
<tr>
<td align="left">switch</td>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=7893079&ss_c=ssc.citiao.link" target="_blank" rel="noopener">synchronized</a></td>
<td align="left">this</td>
<td align="left">throw</td>
<td align="left">throws</td>
</tr>
<tr>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=53860001&ss_c=ssc.citiao.link" target="_blank" rel="noopener">transient</a></td>
<td align="left">try</td>
<td align="left">void</td>
<td align="left">volatile</td>
<td align="left">while</td>
</tr>
</tbody></table>
<p><strong><br /></strong>大致含义：<strong><br /></strong></p>
<table>
<thead>
<tr>
<th align="left"><strong>关键字</strong></th>
<th align="left"><strong>含义</strong></th>
</tr>
</thead>
<tbody><tr>
<td align="left">abstract</td>
<td align="left">表明类或者成员方法具有抽象属性</td>
</tr>
<tr>
<td align="left">assert</td>
<td align="left">断言，用来进行程序调试</td>
</tr>
<tr>
<td align="left">boolean</td>
<td align="left">基本数据类型之一，声明布尔类型的关键字</td>
</tr>
<tr>
<td align="left">break</td>
<td align="left">提前跳出一个块</td>
</tr>
<tr>
<td align="left">byte</td>
<td align="left">基本数据类型之一，字节类型</td>
</tr>
<tr>
<td align="left">case</td>
<td align="left">用在switch语句之中，表示其中的一个分支</td>
</tr>
<tr>
<td align="left">catch</td>
<td align="left">用在异常处理中，用来捕捉异常</td>
</tr>
<tr>
<td align="left">char</td>
<td align="left">基本数据类型之一，字符类型</td>
</tr>
<tr>
<td align="left">class</td>
<td align="left">声明一个类</td>
</tr>
<tr>
<td align="left">const</td>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=167349840&ss_c=ssc.citiao.link" target="_blank" rel="noopener">保留关键字</a>，没有具体含义</td>
</tr>
<tr>
<td align="left">continue</td>
<td align="left">回到一个块的开始处</td>
</tr>
<tr>
<td align="left">default</td>
<td align="left">默认，例如，用在switch语句中，表明一个默认的分支</td>
</tr>
<tr>
<td align="left">do</td>
<td align="left">用在do-while循环结构中</td>
</tr>
<tr>
<td align="left">double</td>
<td align="left">基本数据类型之一，<a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=8487758&ss_c=ssc.citiao.link" target="_blank" rel="noopener">双精度浮点数</a>类型</td>
</tr>
<tr>
<td align="left">else</td>
<td align="left">用在条件语句中，表明当条件不成立时的分支</td>
</tr>
<tr>
<td align="left">enum</td>
<td align="left">枚举</td>
</tr>
<tr>
<td align="left">extends</td>
<td align="left">表明一个类型是另一个类型的子类型，这里常见的类型有类和接口</td>
</tr>
<tr>
<td align="left">final</td>
<td align="left">用来说明最终属性，表明一个类不能派生出子类，或者成员方法不能被覆盖，或者成员域的值不能被改变，用来定义常量</td>
</tr>
<tr>
<td align="left">finally</td>
<td align="left">用于处理异常情况，用来声明一个基本肯定会被执行到的语句块</td>
</tr>
<tr>
<td align="left">float</td>
<td align="left">基本数据类型之一，<a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=54349757&ss_c=ssc.citiao.link" target="_blank" rel="noopener">单精度浮点数</a>类型</td>
</tr>
<tr>
<td align="left">for</td>
<td align="left">一种循环结构的引导词</td>
</tr>
<tr>
<td align="left">goto</td>
<td align="left">保留关键字，没有具体含义</td>
</tr>
<tr>
<td align="left">if</td>
<td align="left">条件语句的引导词</td>
</tr>
<tr>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=7686653&ss_c=ssc.citiao.link" target="_blank" rel="noopener">implements</a></td>
<td align="left">表明一个类实现了给定的接口</td>
</tr>
<tr>
<td align="left">import</td>
<td align="left">表明要访问指定的类或包</td>
</tr>
<tr>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=7762065&ss_c=ssc.citiao.link" target="_blank" rel="noopener">instanceof</a></td>
<td align="left">用来测试一个对象是否是指定类型的实例对象</td>
</tr>
<tr>
<td align="left">int</td>
<td align="left">基本数据类型之一，整数类型</td>
</tr>
<tr>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=3389638&ss_c=ssc.citiao.link" target="_blank" rel="noopener">interface</a></td>
<td align="left">接口</td>
</tr>
<tr>
<td align="left">long</td>
<td align="left">基本数据类型之一，长整数类型</td>
</tr>
<tr>
<td align="left">native</td>
<td align="left">用来声明一个方法是由与计算机相关的语言（如C/C++/<a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=126654&ss_c=ssc.citiao.link" target="_blank" rel="noopener">FORTRAN语言</a>）实现的</td>
</tr>
<tr>
<td align="left">new</td>
<td align="left">用来创建新实例对象</td>
</tr>
<tr>
<td align="left">package</td>
<td align="left">包</td>
</tr>
<tr>
<td align="left">private</td>
<td align="left">一种访问控制方式：私用模式</td>
</tr>
<tr>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=7667016&ss_c=ssc.citiao.link" target="_blank" rel="noopener">protected</a></td>
<td align="left">一种访问控制方式：保护模式</td>
</tr>
<tr>
<td align="left">public</td>
<td align="left">一种访问控制方式：共用模式</td>
</tr>
<tr>
<td align="left">return</td>
<td align="left">从成员方法中返回数据</td>
</tr>
<tr>
<td align="left">short</td>
<td align="left">基本数据类型之一,短整数类型</td>
</tr>
<tr>
<td align="left">static</td>
<td align="left">表明具有静态属性</td>
</tr>
<tr>
<td align="left">strictfp</td>
<td align="left">用来声明FP_strict（单精度或双精度浮点数）表达式遵循IEEE 754算术规范</td>
</tr>
<tr>
<td align="left">super</td>
<td align="left">表明当前对象的父类型的引用或者父类型的构造方法</td>
</tr>
<tr>
<td align="left">switch</td>
<td align="left">分支语句结构的引导词</td>
</tr>
<tr>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=7893079&ss_c=ssc.citiao.link" target="_blank" rel="noopener">synchronized</a></td>
<td align="left">表明一段代码需要同步执行</td>
</tr>
<tr>
<td align="left">this</td>
<td align="left">指向当前实例对象的引用</td>
</tr>
<tr>
<td align="left">throw</td>
<td align="left">抛出一个异常</td>
</tr>
<tr>
<td align="left">throws</td>
<td align="left">声明在当前定义的成员方法中所有需要抛出的异常</td>
</tr>
<tr>
<td align="left"><a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=53860001&ss_c=ssc.citiao.link" target="_blank" rel="noopener">transient</a></td>
<td align="left">声明不用<a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=54553&ss_c=ssc.citiao.link" target="_blank" rel="noopener">序列化</a>的成员域</td>
</tr>
<tr>
<td align="left">try</td>
<td align="left">尝试一个可能抛出异常的程序块</td>
</tr>
<tr>
<td align="left">void</td>
<td align="left">声明当前成员方法没有<a href="https://baike.sogou.com/lemma/ShowInnerLink.htm?lemmaId=581547&ss_c=ssc.citiao.link" target="_blank" rel="noopener">返回值</a></td>
</tr>
<tr>
<td align="left">volatile</td>
<td align="left">表明两个或者多个变量必须同步地发生变化</td>
</tr>
<tr>
<td align="left">while</td>
<td align="left">用在循环结构中</td>
</tr>
</tbody></table>
<p><a name="YrG8Y"></a></p>
<h1 id="数据类型——详情"><a href="#数据类型——详情" class="headerlink" title="数据类型——详情"></a><a href="https://www.runoob.com/java/java-basic-datatypes.html" target="_blank" rel="noopener">数据类型</a>——详情</h1><p>Java语言提供了八种基本类型。六种数字类型（四个整数型，两个浮点型），一种字符类型，还有一种布尔型。<br /><strong>byte：</strong></p>
<ul>
<li>byte 数据类型是8位、有符号的，以二进制补码表示的整数；</li>
<li>最小值是 <strong>-128（-2^7）</strong>；</li>
<li>最大值是 <strong>127（2^7-1）</strong>；</li>
<li>默认值是 <strong>0</strong>；</li>
<li>byte 类型用在大型数组中节约空间，主要代替整数，因为 byte 变量占用的空间只有 int 类型的四分之一；</li>
<li>例子：byte a = 100，byte b = -50。</li>
</ul>
<p><strong>short：</strong></p>
<ul>
<li>short 数据类型是 16 位、有符号的以二进制补码表示的整数</li>
<li>最小值是 <strong>-32768（-2^15）</strong>；</li>
<li>最大值是 <strong>32767（2^15 - 1）</strong>；</li>
<li>Short 数据类型也可以像 byte 那样节省空间。一个short变量是int型变量所占空间的二分之一；</li>
<li>默认值是 <strong>0</strong>；</li>
<li>例子：short s = 1000，short r = -20000。</li>
</ul>
<p><strong>int：</strong></p>
<ul>
<li>int 数据类型是32位、有符号的以二进制补码表示的整数；</li>
<li>最小值是 <strong>-2,147,483,648（-2^31）</strong>；</li>
<li>最大值是 <strong>2,147,483,647（2^31 - 1）</strong>；</li>
<li>一般地整型变量默认为 int 类型；</li>
<li>默认值是 <strong>0</strong> ；</li>
<li>例子：int a = 100000, int b = -200000。</li>
</ul>
<p><strong>long：</strong></p>
<ul>
<li>long 数据类型是 64 位、有符号的以二进制补码表示的整数；</li>
<li>最小值是 <strong>-9,223,372,036,854,775,808（-2^63）</strong>；</li>
<li>最大值是 <strong>9,223,372,036,854,775,807（2^63 -1）</strong>；</li>
<li>这种类型主要使用在需要比较大整数的系统上；</li>
<li>默认值是 <strong>0L</strong>；</li>
<li>例子： long a = 100000L，Long b = -200000L。<br />“L”理论上不分大小写，但是若写成”l”容易与数字”1”混淆，不容易分辩。所以最好大写。</li>
</ul>
<p><strong>float：</strong></p>
<ul>
<li>float 数据类型是单精度、32位、符合IEEE 754标准的浮点数；</li>
<li>float 在储存大型浮点数组的时候可节省内存空间；</li>
<li>默认值是 <strong>0.0f</strong>；</li>
<li>浮点数不能用来表示精确的值，如货币；</li>
<li>例子：float f1 = 234.5f。</li>
</ul>
<p><strong>double：</strong></p>
<ul>
<li>double 数据类型是双精度、64 位、符合IEEE 754标准的浮点数；</li>
<li>浮点数的默认类型为double类型；</li>
<li>double类型同样不能表示精确的值，如货币；</li>
<li>默认值是 <strong>0.0d</strong>；</li>
<li>例子：double d1 = 123.4。</li>
</ul>
<p><strong>boolean：</strong></p>
<ul>
<li>boolean数据类型表示一位的信息；</li>
<li>只有两个取值：true 和 false；</li>
<li>这种类型只作为一种标志来记录 true/false 情况；</li>
<li>默认值是 <strong>false</strong>；</li>
<li>例子：boolean one = true。</li>
</ul>
<p><strong>char：</strong></p>
<ul>
<li><p>char类型是一个单一的 16 位 <a href="https://baike.sogou.com/v10855091.htm?fromTitle=unicode%E7%A0%81" target="_blank" rel="noopener">Unicode</a> 字符；</p>
</li>
<li><p>最小值是 <strong>\u0000</strong>（即为0）；</p>
</li>
<li><p>最大值是 <strong>\uffff</strong>（即为65,535）；</p>
</li>
<li><p>char 数据类型可以储存任何字符；</p>
</li>
<li><p>例子：char letter = ‘A’;。<br><a name="gWdYQ"></a></p>
<h1 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a><a href="https://www.runoob.com/java/java-operators.html" target="_blank" rel="noopener">运算符</a></h1></li>
<li><p>算术运算符</p>
</li>
<li><p>关系运算符</p>
</li>
<li><p>位运算符</p>
</li>
<li><p>逻辑运算符</p>
</li>
<li><p>赋值运算符</p>
</li>
<li><p>其他运算符<br><a name="pIWx8"></a></p>
<h2 id="算术运算符"><a href="#算术运算符" class="headerlink" title="算术运算符"></a>算术运算符</h2><table>
<thead>
<tr>
<th align="left">操作符</th>
<th align="left">描述</th>
<th align="left">例子</th>
</tr>
</thead>
<tbody><tr>
<td align="left">+</td>
<td align="left">加法 - 相加运算符两侧的值</td>
<td align="left">A + B 等于 30</td>
</tr>
<tr>
<td align="left">-</td>
<td align="left">减法 - 左操作数减去右操作数</td>
<td align="left">A – B 等于 -10</td>
</tr>
<tr>
<td align="left">*</td>
<td align="left">乘法 - 相乘操作符两侧的值</td>
<td align="left">A * B等于200</td>
</tr>
<tr>
<td align="left">/</td>
<td align="left">除法 - 左操作数除以右操作数</td>
<td align="left">B / A等于2</td>
</tr>
<tr>
<td align="left">％</td>
<td align="left">取余 - 左操作数除以右操作数的余数</td>
<td align="left">B%A等于0</td>
</tr>
<tr>
<td align="left">++</td>
<td align="left">自增: 操作数的值增加1</td>
<td align="left">B++ 或 ++B 等于 21（区别详见下文）</td>
</tr>
<tr>
<td align="left">–</td>
<td align="left">自减: 操作数的值减少1</td>
<td align="left">B– 或 –B 等于 19（区别详见下文）</td>
</tr>
</tbody></table>
</li>
</ul>
<p><a name="0JbLc"></a></p>
<h2 id="关系运算符"><a href="#关系运算符" class="headerlink" title="关系运算符"></a>关系运算符</h2><p>下表为Java支持的关系运算符<br />表格中的实例整数变量A的值为10，变量B的值为20：</p>
<table>
<thead>
<tr>
<th align="left">运算符</th>
<th align="left">描述</th>
<th align="left">例子</th>
</tr>
</thead>
<tbody><tr>
<td align="left">==</td>
<td align="left">检查如果两个操作数的值是否相等，如果相等则条件为真。</td>
<td align="left">（A == B）为假。</td>
</tr>
<tr>
<td align="left">!=</td>
<td align="left">检查如果两个操作数的值是否相等，如果值不相等则条件为真。</td>
<td align="left">(A != B) 为真。</td>
</tr>
<tr>
<td align="left">&gt;</td>
<td align="left">检查左操作数的值是否大于右操作数的值，如果是那么条件为真。</td>
<td align="left">（A&gt; B）为假。</td>
</tr>
<tr>
<td align="left">&lt;</td>
<td align="left">检查左操作数的值是否小于右操作数的值，如果是那么条件为真。</td>
<td align="left">（A &lt;B）为真。</td>
</tr>
<tr>
<td align="left">&gt;=</td>
<td align="left">检查左操作数的值是否大于或等于右操作数的值，如果是那么条件为真。</td>
<td align="left">（A&gt; = B）为假。</td>
</tr>
<tr>
<td align="left">&lt;=</td>
<td align="left">检查左操作数的值是否小于或等于右操作数的值，如果是那么条件为真。</td>
<td align="left">（A &lt;= B）为真。</td>
</tr>
</tbody></table>
<p><a name="Sman3"></a></p>
<h2 id="位运算符"><a href="#位运算符" class="headerlink" title="位运算符"></a>位运算符</h2><p>Java定义了位运算符，应用于整数类型(int)，长整型(long)，短整型(short)，字符型(char)，和字节型(byte)等类型。<br />位运算符作用在所有的位上，并且按位运算。假设a = 60，b = 13;它们的二进制格式表示将如下：<br />A = 0011 1100<br />B = 0000 1101<br />—————–<br />A&amp;B = 0000 1100<br />A | B = 0011 1101<br />A ^ B = 0011 0001<br />~A= 1100 0011<br />下表列出了位运算符的基本运算，假设整数变量 A 的值为 60 和变量 B 的值为 13：</p>
<table>
<thead>
<tr>
<th align="left">操作符</th>
<th align="left">描述</th>
<th align="left">例子</th>
</tr>
</thead>
<tbody><tr>
<td align="left">＆</td>
<td align="left">如果相对应位都是1，则结果为1，否则为0</td>
<td align="left">（A＆B），得到12，即0000 1100</td>
</tr>
<tr>
<td align="left">&#124;</td>
<td align="left">如果相对应位都是 0，则结果为 0，否则为 1</td>
<td align="left">（A &#124; B）得到61，即 0011 1101</td>
</tr>
<tr>
<td align="left">^</td>
<td align="left">如果相对应位值相同，则结果为0，否则为1</td>
<td align="left">（A ^ B）得到49，即 0011 0001</td>
</tr>
<tr>
<td align="left">〜</td>
<td align="left">按位取反运算符翻转操作数的每一位，即0变成1，1变成0。</td>
<td align="left">（〜A）得到-61，即1100 0011</td>
</tr>
<tr>
<td align="left">&lt;&lt;</td>
<td align="left">按位左移运算符。左操作数按位左移右操作数指定的位数。</td>
<td align="left">A &lt;&lt; 2得到240，即 1111 0000</td>
</tr>
<tr>
<td align="left">&gt;&gt;</td>
<td align="left">按位右移运算符。左操作数按位右移右操作数指定的位数。</td>
<td align="left">A &gt;&gt; 2得到15即 1111</td>
</tr>
<tr>
<td align="left">&gt;&gt;&gt;</td>
<td align="left">按位右移补零操作符。左操作数的值按右操作数指定的位数右移，移动得到的空位以零填充。</td>
<td align="left">A&gt;&gt;&gt;2得到15即0000 1111</td>
</tr>
</tbody></table>
<p><a name="Bsets"></a></p>
<h2 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h2><p>下表列出了逻辑运算符的基本运算，假设布尔变量A为真，变量B为假</p>
<table>
<thead>
<tr>
<th align="left">操作符</th>
<th align="left">描述</th>
<th align="left">例子</th>
</tr>
</thead>
<tbody><tr>
<td align="left">&amp;&amp;</td>
<td align="left">称为逻辑与运算符。当且仅当两个操作数都为真，条件才为真。</td>
<td align="left">（A &amp;&amp; B）为假。</td>
</tr>
<tr>
<td align="left">&#124; &#124;</td>
<td align="left">称为逻辑或操作符。如果任何两个操作数任何一个为真，条件为真。</td>
<td align="left">（A &#124; &#124; B）为真。</td>
</tr>
<tr>
<td align="left">！</td>
<td align="left">称为逻辑非运算符。用来反转操作数的逻辑状态。如果条件为true，则逻辑非运算符将得到false。</td>
<td align="left">！（A &amp;&amp; B）为真。</td>
</tr>
</tbody></table>
<p><a name="bMjyq"></a></p>
<h2 id="短路逻辑运算符"><a href="#短路逻辑运算符" class="headerlink" title="短路逻辑运算符"></a>短路逻辑运算符</h2><p>当使用与逻辑运算符时，在两个操作数都为true时，结果才为true，但是当得到第一个操作为false时，其结果就必定是false，这时候就不会再判断第二个操作了。<br><a name="VGREG"></a></p>
<h3 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LuoJi</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> a = <span class="number">5</span>;<span class="comment">//定义一个变量；</span></span><br><span class="line">        <span class="keyword">boolean</span> b = (a&lt;<span class="number">4</span>)&amp;&amp;(a++&lt;<span class="number">10</span>);</span><br><span class="line">        System.out.println(<span class="string">"使用短路逻辑运算符的结果为"</span>+b);</span><br><span class="line">        System.out.println(<span class="string">"a的结果为"</span>+a);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行结果为：<br />使用短路逻辑运算符的结果为false<br />a的结果为5<br><a name="hSkJ8"></a></p>
<h2 id="赋值运算符"><a href="#赋值运算符" class="headerlink" title="赋值运算符"></a>赋值运算符</h2><p>下面是Java语言支持的赋值运算符：</p>
<table>
<thead>
<tr>
<th align="left">操作符</th>
<th align="left">描述</th>
<th align="left">例子</th>
</tr>
</thead>
<tbody><tr>
<td align="left">=</td>
<td align="left">简单的赋值运算符，将右操作数的值赋给左侧操作数</td>
<td align="left">C = A + B将把A + B得到的值赋给C</td>
</tr>
<tr>
<td align="left">+ =</td>
<td align="left">加和赋值操作符，它把左操作数和右操作数相加赋值给左操作数</td>
<td align="left">C + = A等价于C = C + A</td>
</tr>
<tr>
<td align="left">- =</td>
<td align="left">减和赋值操作符，它把左操作数和右操作数相减赋值给左操作数</td>
<td align="left">C - = A等价于C = C - A</td>
</tr>
<tr>
<td align="left">* =</td>
<td align="left">乘和赋值操作符，它把左操作数和右操作数相乘赋值给左操作数</td>
<td align="left">C * = A等价于C = C * A</td>
</tr>
<tr>
<td align="left">/ =</td>
<td align="left">除和赋值操作符，它把左操作数和右操作数相除赋值给左操作数</td>
<td align="left">C / = A，C 与 A 同类型时等价于 C = C / A</td>
</tr>
<tr>
<td align="left">（％）=</td>
<td align="left">取模和赋值操作符，它把左操作数和右操作数取模后赋值给左操作数</td>
<td align="left">C％= A等价于C = C％A</td>
</tr>
<tr>
<td align="left">&lt;&lt; =</td>
<td align="left">左移位赋值运算符</td>
<td align="left">C &lt;&lt; = 2等价于C = C &lt;&lt; 2</td>
</tr>
<tr>
<td align="left">&gt;&gt; =</td>
<td align="left">右移位赋值运算符</td>
<td align="left">C &gt;&gt; = 2等价于C = C &gt;&gt; 2</td>
</tr>
<tr>
<td align="left">＆=</td>
<td align="left">按位与赋值运算符</td>
<td align="left">C＆= 2等价于C = C＆2</td>
</tr>
<tr>
<td align="left">^ =</td>
<td align="left">按位异或赋值操作符</td>
<td align="left">C ^ = 2等价于C = C ^ 2</td>
</tr>
<tr>
<td align="left">&#124; =</td>
<td align="left">按位或赋值操作符</td>
<td align="left">C &#124; = 2等价于C = C &#124; 2</td>
</tr>
</tbody></table>
<p><a name="6GS8L"></a></p>
<h2 id="条件运算符（-）"><a href="#条件运算符（-）" class="headerlink" title="条件运算符（?:）"></a>条件运算符（?:）</h2><p>条件运算符也被称为三元运算符。该运算符有3个操作数，并且需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">variable x = (expression) ? value <span class="keyword">if</span> <span class="keyword">true</span> : value <span class="keyword">if</span> <span class="keyword">false</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">实例</span><br><span class="line">Test.java 文件代码：</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> </span>&#123;</span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">      <span class="keyword">int</span> a , b;</span><br><span class="line">      a = <span class="number">10</span>;</span><br><span class="line">      <span class="comment">// 如果 a 等于 1 成立，则设置 b 为 20，否则为 30</span></span><br><span class="line">      b = (a == <span class="number">1</span>) ? <span class="number">20</span> : <span class="number">30</span>;</span><br><span class="line">      System.out.println( <span class="string">"Value of b is : "</span> +  b );</span><br><span class="line"> </span><br><span class="line">      <span class="comment">// 如果 a 等于 10 成立，则设置 b 为 20，否则为 30</span></span><br><span class="line">      b = (a == <span class="number">10</span>) ? <span class="number">20</span> : <span class="number">30</span>;</span><br><span class="line">      System.out.println( <span class="string">"Value of b is : "</span> + b );</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//结果为：</span></span><br><span class="line"><span class="comment">// Value of b is : 30</span></span><br><span class="line"><span class="comment">// Value of b is : 20</span></span><br></pre></td></tr></table></figure>
<p><a name="F6dGA"></a></p>
<h2 id="instanceof-运算符"><a href="#instanceof-运算符" class="headerlink" title="instanceof 运算符"></a>instanceof 运算符</h2><p>该运算符用于操作对象实例，检查该对象是否是一个特定类型（类类型或接口类型）。<br />instanceof运算符使用格式如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">( Object reference variable ) <span class="keyword">instanceof</span>  (<span class="class"><span class="keyword">class</span>/<span class="title">interface</span> <span class="title">type</span>)</span></span><br></pre></td></tr></table></figure>
<p>如果运算符左侧变量所指的对象，是操作符右侧类或接口(class/interface)的一个对象，那么结果为真。<br />下面是一个例子：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String name = <span class="string">"James"</span>;</span><br><span class="line"><span class="keyword">boolean</span> result = name <span class="keyword">instanceof</span> String; <span class="comment">// 由于 name 是 String 类型，所以返回真</span></span><br></pre></td></tr></table></figure>
<p>如果被比较的对象兼容于右侧类型,该运算符仍然返回true。<br />看下面的例子：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Vehicle</span> </span>&#123;&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="keyword">extends</span> <span class="title">Vehicle</span> </span>&#123;</span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">      Vehicle a = <span class="keyword">new</span> Car();</span><br><span class="line">      <span class="keyword">boolean</span> result =  a <span class="keyword">instanceof</span> Car;</span><br><span class="line">      System.out.println( result);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a name="n9hBG"></a></p>
<h2 id="Java运算符优先级"><a href="#Java运算符优先级" class="headerlink" title="Java运算符优先级"></a>Java运算符优先级</h2><p>当多个运算符出现在一个表达式中，谁先谁后呢？这就涉及到运算符的优先级别的问题。在一个多运算符的表达式中，运算符优先级不同会导致最后得出的结果差别甚大。<br />例如，（1+3）＋（3+2）*2，这个表达式如果按加号最优先计算，答案就是 18，如果按照乘号最优先，答案则是 14。<br />再如，x = 7 + 3 * 2;这里x得到13，而不是20，因为乘法运算符比加法运算符有较高的优先级，所以先计算3 * 2得到6，然后再加7。<br />下表中具有最高优先级的运算符在的表的最上面，最低优先级的在表的底部。</p>
<table>
<thead>
<tr>
<th align="left">类别</th>
<th align="left">操作符</th>
<th align="left">关联性</th>
</tr>
</thead>
<tbody><tr>
<td align="left">后缀</td>
<td align="left">() [] . (点操作符)</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">一元</td>
<td align="left">+ + - ！〜</td>
<td align="left">从右到左</td>
</tr>
<tr>
<td align="left">乘性</td>
<td align="left">* /％</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">加性</td>
<td align="left">+ -</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">移位</td>
<td align="left">&gt;&gt; &gt;&gt;&gt;  &lt;&lt;</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">关系</td>
<td align="left">&gt;&gt; = &lt;&lt; =</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">相等</td>
<td align="left">==  !=</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">按位与</td>
<td align="left">＆</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">按位异或</td>
<td align="left">^</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">按位或</td>
<td align="left">&#124;</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">逻辑与</td>
<td align="left">&amp;&amp;</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">逻辑或</td>
<td align="left">&#124; &#124;</td>
<td align="left">左到右</td>
</tr>
<tr>
<td align="left">条件</td>
<td align="left">？：</td>
<td align="left">从右到左</td>
</tr>
<tr>
<td align="left">赋值</td>
<td align="left">= + = - = * = / =％= &gt;&gt; = &lt;&lt; =＆= ^ = &#124; =</td>
<td align="left">从右到左</td>
</tr>
<tr>
<td align="left">逗号</td>
<td align="left">，</td>
<td align="left">左到右</td>
</tr>
</tbody></table>

    </div>

    
    
    
        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/blog/uploads/vx.png" alt="刘正星 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/blog/uploads/alipay.jpg" alt="刘正星 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

            <div class="social-item">
              <a target="_blank" class="social-link" href="/atom.xml">
                <span class="icon">
                  <i class="fa fa-rss"></i>
                </span>

                <span class="label">RSS</span>
              </a>
            </div>
    </div>
  </div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/blog/tags/%E5%8F%98%E9%87%8F-%E7%B1%BB%E5%9E%8B/" rel="tag"><i class="fa fa-tag"></i> -变量 -类型</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/03/04/JAVA%E9%AB%98%E6%95%88%E7%BD%91%E7%BB%9C%E5%BA%93Okhttp3/" rel="prev" title="JAVA高效网络库Okhttp3">
      <i class="fa fa-chevron-left"></i> JAVA高效网络库Okhttp3
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  
   <div>
      
        ﻿
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/blog/2020/03/07/%E5%8F%98%E9%87%8F%E5%92%8C%E7%B1%BB%E5%9E%8B/">变量和类型</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 刘正星 的个人博客">刘正星</a></p>
  <p><span>发布时间:</span>2020年03月07日 - 22:03</p>
  <p><span>最后更新:</span>2020年03月07日 - 22:03</p>
  <p><span>原始链接:</span><a href="/blog/2020/03/07/%E5%8F%98%E9%87%8F%E5%92%8C%E7%B1%BB%E5%9E%8B/" title="变量和类型">https://gitee.com/lzx411559669/2020/03/07/%E5%8F%98%E9%87%8F%E5%92%8C%E7%B1%BB%E5%9E%8B/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://gitee.com/lzx411559669/2020/03/07/%E5%8F%98%E9%87%8F%E5%92%8C%E7%B1%BB%E5%9E%8B/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>

      
   </div>
  </div>


          </div>
          
    
  <div class="comments">
    <div id="lv-container" data-id="city" data-uid="MTAyMC80ODg4Ny8yNTM4MQ=="></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#变量"><span class="nav-number">1.</span> <span class="nav-text">变量</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Java语言支持的变量类型有："><span class="nav-number">1.1.</span> <span class="nav-text">Java语言支持的变量类型有：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类变量（静态变量）"><span class="nav-number">1.2.</span> <span class="nav-text">类变量（静态变量）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Java-局部变量"><span class="nav-number">1.3.</span> <span class="nav-text">Java 局部变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实例变量"><span class="nav-number">1.4.</span> <span class="nav-text">实例变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Java-中静态变量和实例变量区别"><span class="nav-number">1.5.</span> <span class="nav-text">Java 中静态变量和实例变量区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#成员变量、局部变量、静态变量的区别"><span class="nav-number">1.6.</span> <span class="nav-text">成员变量、局部变量、静态变量的区别</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#关键字"><span class="nav-number">2.</span> <span class="nav-text">关键字</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#数据类型——详情"><span class="nav-number">3.</span> <span class="nav-text">数据类型——详情</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#运算符"><span class="nav-number">4.</span> <span class="nav-text">运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#算术运算符"><span class="nav-number">4.1.</span> <span class="nav-text">算术运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#关系运算符"><span class="nav-number">4.2.</span> <span class="nav-text">关系运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#位运算符"><span class="nav-number">4.3.</span> <span class="nav-text">位运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#逻辑运算符"><span class="nav-number">4.4.</span> <span class="nav-text">逻辑运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#短路逻辑运算符"><span class="nav-number">4.5.</span> <span class="nav-text">短路逻辑运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#实例"><span class="nav-number">4.5.1.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#赋值运算符"><span class="nav-number">4.6.</span> <span class="nav-text">赋值运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#条件运算符（-）"><span class="nav-number">4.7.</span> <span class="nav-text">条件运算符（?:）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#instanceof-运算符"><span class="nav-number">4.8.</span> <span class="nav-text">instanceof 运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Java运算符优先级"><span class="nav-number">4.9.</span> <span class="nav-text">Java运算符优先级</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="刘正星"
      src="/blog/uploads/me.jpg">
  <p class="site-author-name" itemprop="name">刘正星</p>
  <div class="site-description" itemprop="description">一名不知名的小菜鸡</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">6</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://gitee.com/lzx411559669" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;lzx411559669"><i class="fa fa-fw fa-gitee"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://github.com/lzx411559669" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lzx411559669" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/blog/411559669@qq.com" title="E-Mail → 411559669@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://wpa.qq.com/msgrd?v=3&uin=411559669&site=qq&menu=yes" title="QQ → http:&#x2F;&#x2F;wpa.qq.com&#x2F;msgrd?v&#x3D;3&amp;uin&#x3D;411559669&amp;site&#x3D;qq&amp;menu&#x3D;yes" rel="noopener" target="_blank"><i class="fa fa-fw fa-qq"></i>QQ</a>
      </span>
  </div>



      </div>

     <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=471565537&auto=0&height=66"></iframe>

<!--
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=220 src="//music.163.com/outchain/player?type=0&id=4894835465&auto=0&height=430"></iframe>
-->
<iframe scrolling="no" src="https://tianqiapi.com/api.php?style=tx&skin=sogou" frameborder="0" width="100%" height="50" allowtransparency="true"></iframe>
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">刘正星</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">39k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">35 分钟</span>
</div>
<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.2.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.7.2
  </div>
-->

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<!--
<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>
  <span class="post-meta-divider">|</span>
<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共16.2k字</span>
</div>

-->

<span id="busuanzi_container_site_pv" > 总访问量 <span id="busuanzi_value_site_pv"></span> 次 </span>
<span id="busuanzi_container_site_uv" >| 总访客数 <span id="busuanzi_value_site_uv"></span> 人 </span>
<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">| 博客全站共16.2k字</span>
</div>


        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

<script>
NexT.utils.loadComments(document.querySelector('#lv-container'), () => {
  window.livereOptions = {
    refer: location.pathname.replace(CONFIG.root, '').replace('index.html', '')
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
});
</script>

</body>
</html>
<!-- ��̬���� -->
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
<!-- ҳ����С���� -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>